//-----------------------------------------------------------------------------
// FILENAME : EEPROM.c
// AUTHOR : i-VITAL Hung
// (c) Copyright 2018.08.30, i-VITAL TECHNOLOGY CO., LTD.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
// #include "Glo.h"
#include "EEPROM.h"


//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Global Constants 
//-----------------------------------------------------------------------------
#define ISP_Num        			8   

#define DATA_Addr_BASE        	0x00    	// dataBase Address
#define DATA_Addr_BASE_END      0x7F    	// the end dataBase Address
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Global Variables 
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Function Prototypes 
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// EEPROM work
//-----------------------------------------------------------------------------
// void EEPROM_work(void)
// {
// 	if(f_EEPROM==1)
// 	{
// 		f_EEPROM=0;
// 		EEPROM_Write();	
// 	}		
// }
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//Function: write data to EEPROM 
//-----------------------------------------------------------------------------
void PR_EEPROM_WR(unsigned char GU8V_EEPROM_ADDR,unsigned char GU8V_WR_DATA)
{
		_eea=GU8V_EEPROM_ADDR;	// user defined address 
		_eed=GU8V_WR_DATA;		// user defined data
		_mp1=0x40;      		// setup memory pointer MP1
								// MP1 points to EEC register
		_bp=1;					// setup Bank Pointer BP
	    if(_emi)
		{
	       _emi = 0;
		   iar1_3 = 1;			// set WREN bit, enable write operations
		   iar1_2 = 1;			// tart Write Cycle - set WR bit
		   						// after set WREN bit
		   _emi = 1;
		}
		else
		{
		   iar1_3 = 1;
		   iar1_2 = 1;	
		}
		while(iar1_2);			// check for write cycle end		
		_iar1 = 0x00;			// disable EEPROM write
		_bp=0;
}
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
//Function: read data from EEPROM
//-----------------------------------------------------------------------------
unsigned char PR_EEPROM_RD(unsigned char GU8V_EEPROM_ADDR)
{
	uint8_t	GU8V_RD_DATA;
	
	_eea=GU8V_EEPROM_ADDR;	// user defined address
	_mp1=0x40;				// setup memory pointer MP1
							// MP1 points to EEC register
	_bp=1;					// setup Bank Pointer BP
	if(_emi)
	{
	   _emi = 0;
	   iar1_1 = 1;			// set RDEN bit, enable read operations
	   iar1_0 = 1;			// start Read Cycle - set RD bit
	   _emi = 1;
	}
	else
	{
	   iar1_1 = 1;
	   iar1_0 = 1;	
	}
	while(iar1_0);			// check for read cycle end
	_iar1=0x00;				// disable EEPROM write
	_bp=0;
	GU8V_RD_DATA=_eed;		// move read data to register		

	return GU8V_RD_DATA;	
	
}
//-----------------------------------------------------------------------------


